﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication18
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    private void button1_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1();
            form.Show();
            Graphics g = this.CreateGraphics();
            Pen mypen = new Pen(Color.Blue, 3);
            Point oo1 = new Point(30, this.ClientSize.Height - 100);
            Point oo2 = new Point(this.ClientSize.Width - 50, this.ClientSize.Height - 100);
            g.DrawLine(mypen, oo1, oo2);
            Point oo3 = new Point(30, 30);
            g.DrawLine(mypen, oo1, oo3);
            Font f = new Font("宋体", 12, FontStyle.Bold);
            g.DrawString("x", f, mypen.Brush, oo2);
            g.DrawString("y", f, mypen.Brush, 10, 10);
            int x1, x2, y1;
            double a, y2;
            x1 = x2 = 0;
            y1 = 0; y2 = this.ClientSize.Height - 100;
            for (x2 = 0; x2 < this.ClientSize.Width; x2++)
            {
                a = 2 * Math.PI * x2 / (this.ClientSize.Width);
                y2 = Math.Sin(a);
                y2 = (1 - y2) * (this.ClientSize.Height - 100) / 2;
                g.DrawLine(mypen, x1 + 30, (float)y1, x2 + 30, (float)y2);
                x1 = Convert.ToInt32(x2);
                y1 = Convert.ToInt32(y2);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Form1 form = new Form1();
            form.Show();
            Graphics g = this.CreateGraphics();
            Rectangle r = new Rectangle(50, 50, 200, 100);
            Pen p = new Pen(Color.Red, 3);
            Brush b = p.Brush;
            g.FillPie(p.Brush, r, 0, 60);
            g.FillPie(b, r, 60, 150);
            b = new SolidBrush(Color.Yellow);
            g.FillPie(b, r, 210, 150);
        }
        private void Fill(int[] percent, Color[] percolor)
        {
            Graphics g = this.CreateGraphics();
            Rectangle r = new Rectangle(50, 50, 200, 100);
            Brush b;
            int beginAngle = 0;
            for (int i = 0; i < percent.GetUpperBound(0); i++)
            {
                b = new SolidBrush(percolor[i]);
                g.FillPie(b, r, beginAngle, percent[i]);
                beginAngle += percent[i];

            }
        }
        public byte[] getfilebytes(string filename)
        {
            if (filename == "")
                return null;
            try
            {
                FileStream filestream = new FileStream(filename, FileMode.Open,
                FileAccess.Read);
                BinaryReader binaryreader = new BinaryReader(filestream);
                byte[] filebytes = binaryreader.ReadBytes((int)filestream.Length);
                binaryreader.Close();
                filestream.Close();
                return filebytes;
            }
            catch
            {
                return null;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {            
            try
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.bmp)|*.bmp";
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    byte[] picbinary = getfilebytes(openfile.FileName);                    
                    MemoryStream mempicstream = new MemoryStream(picbinary);                  
                    pictureBox1.Image = Image.FromStream(mempicstream);                   
                    mempicstream.Dispose();
                    mempicstream.Close();
                }
            }
            catch (Exception m)
            {
                MessageBox.Show("读取图片出错，可能的问题是：" + Convert.ToString(m), "错误提示");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {            
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            Font f = new Font("隶书", 80, FontStyle.Italic);
            Pen p = new Pen(Color.OrangeRed);
            g.DrawString("花开花落", f, p.Brush, 0, 0);
            p.Dispose();
            g.Dispose();
        }

        
    }
}
